home *** CD-ROM | disk | FTP | other *** search
-
- #include <DeskBus.h>
-
-
- #include "PrefResource.h"
-
- #define kPi 3.1415926535898
-
-
- #define kMouseDialogResID 128
-
- enum
- {
- eRotationSliderID = 1,
- eRotationPicID = 3,
- eAutosenseCheckboxID
- };
-
-
- enum
- {
- eBasePicID = 200
-
- };
-
- Boolean gQuitFlag = false;
-
-
- enum {
- kAppleMenuID = 128,
- kFileMenuID,
- kEditMenuID
- };
-
- enum {
- kAboutMenuItem = 1
- };
-
- enum {
- kCloseMenuItem = 1,
- kQuitMenuItem
- };
-
- static MenuHandle gAppleMenu, gFileMenu, gEditMenu;
-
-
- /*----------------------------------------------------------------------------
- GetMousePrefs
-
- ----------------------------------------------------------------------------*/
- static OSErr GetMousePrefs(TPrefsData *outPrefsData)
- {
- TPrefsDataPtr globalDataPtr;
- OSErr err;
-
- err = Gestalt(kDataGestaltSelector, (long *)&globalDataPtr);
- if (err != noErr) return err;
-
- *outPrefsData = *globalDataPtr;
- return noErr;
- }
-
- /*----------------------------------------------------------------------------
- SetMousePrefs
-
- ----------------------------------------------------------------------------*/
-
- static OSErr SetMousePrefs(double thetaDelta)
- {
- TPrefsDataPtr globalDataPtr;
- OSErr err;
-
- err = Gestalt(kDataGestaltSelector, (long *)&globalDataPtr);
- if (err != noErr) return err;
-
- globalDataPtr->thetaDelta = thetaDelta;
- return noErr;
- }
-
-
- //------------------------DrawBeast--------------------
- static pascal void DrawMousePicture (DialogPtr theDialog, short itemNo)
- {
- PicHandle mousePic;
- short itemType, whichSetting;
- short picID;
- Rect box;
- Handle item;
-
- //Find which beast
- GetDialogItem(theDialog, eRotationSliderID, &itemType, &item, &box);
- whichSetting = GetControlValue((ControlHandle)item);
-
- picID = eBasePicID + whichSetting - 1;
-
- GetDialogItem(theDialog, itemNo, &itemType, &item, &box);
-
- mousePic = GetPicture(picID);
- if (mousePic)
- {
- HNoPurge((Handle)mousePic);
- DrawPicture(mousePic, &box);
- HPurge((Handle)mousePic);
- ReleaseResource((Handle)mousePic);
- }
- }
-
-
- #pragma mark -
-
- /*----------------------------------------------------------------------------
- DisplayMouseDialog
-
- ----------------------------------------------------------------------------*/
-
- static OSErr DisplayMouseDialog(TPrefsData *ioPrefs)
- {
- DialogPtr dlg;
- Rect box;
- short itemType;
- short controlValue = 3;
- double thetaDelta;
- Handle itemHandle;
- UserItemUPP picDrawProc;
-
- dlg = GetNewDialog(kMouseDialogResID, nil, (WindowPtr)-1);
- if (!dlg) return resNotFound;
-
- picDrawProc = NewUserItemProc(DrawMousePicture);
-
- GetDialogItem(dlg, eRotationPicID, &itemType, &itemHandle, &box);
- SetDialogItem(dlg, eRotationPicID, itemType, (Handle)picDrawProc, &box);
-
- thetaDelta = ioPrefs->thetaDelta;
-
- if (thetaDelta <= -kPi / 4.0)
- controlValue = 5;
- else if (thetaDelta <= -kPi / 8.0)
- controlValue = 4;
- else if (thetaDelta <= 0.0)
- controlValue = 3;
- else if (thetaDelta <= kPi / 8.0)
- controlValue = 2;
- else
- controlValue = 1;
-
- GetDialogItem(dlg, eRotationSliderID, &itemType, &itemHandle, &box);
- SetControlValue((ControlHandle)itemHandle, controlValue);
-
- ShowWindow(dlg);
- return noErr;
- }
-
-
-
- static void ShowAboutBox(void)
- {
- SysBeep(2);
- }
-
-
-
- #pragma mark -
-
- /*-------------------------DoIdleEvent-------------------------*/
- static Boolean HandleDialogEvent(WindowPtr dlg, short itemHit)
- {
- Rect box;
- Handle itemHandle;
- short itemType;
- GrafPtr oldPort;
- Boolean handled = false;
-
- GetPort(&oldPort);
- SetPort(dlg);
-
- switch (itemHit)
- {
- case eRotationSliderID:
- {
- short whichSetting;
- double theta = 0.0;
-
- GetDialogItem(dlg, eRotationPicID, &itemType, &itemHandle, &box);
- InvalRect(&box);
-
- GetDialogItem(dlg, eRotationSliderID, &itemType, &itemHandle, &box);
- whichSetting = GetControlValue((ControlHandle)itemHandle);
-
- switch (whichSetting)
- {
- case 1: // -60
- theta = kPi / 4.0;
- break;
- case 2:
- theta = kPi / 8.0;
- break;
- default:
- case 3:
- theta = 0.0;
- break;
- case 4:
- theta = -kPi / 8.0;
- break;
- case 5:
- theta = -kPi / 4.0;
- break;
- }
-
- SetMousePrefs(theta);
- }
- break;
-
- case eAutosenseCheckboxID:
- {
- ControlHandle control;
- short curValue;
-
- GetDialogItem(dlg, eAutosenseCheckboxID, &itemType, &(Handle)control, &box);
- curValue = GetControlValue(control);
- SetControlValue(control, !curValue);
- }
- break;
-
- }
-
- SetPort(oldPort);
- return handled;
- }
-
-
- /*-------------------------DoIdleEvent-------------------------*/
- static void DoIdleEvent(void)
- {
-
-
- }
-
-
- /*-------------------------DoUpdateEvent-------------------------*/
- static void HandleWindowUpdate(WindowPtr wind)
- {
-
-
- }
-
-
- /*-------------------------DoUpdateEvent-------------------------*/
- static Boolean DoClose(WindowPtr wind)
- {
- CloseWindow(wind);
- return true;
- }
-
- /*-------------------------DoUpdateEvent-------------------------*/
- static void DoUpdateEvent(EventRecord localEvent)
- {
- WindowPtr wind;
- GrafPtr oldPort;
-
- GetPort(&oldPort);
-
- wind = (WindowPtr)localEvent.message;
-
- SetPort(wind);
- BeginUpdate(wind);
-
- HandleWindowUpdate(wind);
-
- EndUpdate(wind);
- SetPort(oldPort);
- }
-
-
- /*-------------------------DoMenuSelection-------------------------*/
- static Boolean DoMenuSelection (long retVal)
- {
- WindowPtr wind;
- short menuID, itemID;
- Str255 itemStr;
- OSErr err = noErr;
- Boolean returnVal = false;
-
- wind = FrontWindow();
-
- menuID = HiWord (retVal);
- itemID = LoWord (retVal);
-
- switch (menuID) {
- case kAppleMenuID:
- if (itemID == kAboutMenuItem)
- ShowAboutBox (); /* Show the about box */
- else {
- GetMenuItemText(GetMenuHandle(kAppleMenuID), itemID, itemStr);
- OpenDeskAcc(itemStr);
- }
- break;
-
- case kFileMenuID:
- switch (itemID)
- {
- case kCloseMenuItem:
- returnVal = DoClose(wind);
- break;
-
- case kQuitMenuItem:
- returnVal = true;
- break;
-
- default:
- break;
- }
- break;
- case kEditMenuID:
- switch (itemID)
- {
- default:
- break;
- }
- break;
-
- default:
- break;
- }
-
- HiliteMenu(0);
-
- return returnVal;
- }
-
-
-
- /*-------------------------DoActivateEvent-------------------------*/
- static void DoActivateEvent(EventRecord localEvent)
- {
- Boolean activating;
-
- activating = (localEvent.modifiers & activeFlag) != 0;
- }
-
-
- /*-------------------------DoOSEvent-------------------------*/
- static void DoOSEvent(EventRecord localEvent)
- {
- Boolean resuming;
-
- if (((localEvent.message >> 24) & 0xFF) != suspendResumeMessage) return;
-
- resuming = (localEvent.message & 1) == resumeFlag;
- }
-
-
-
- /*-------------------------HandleMouseDown-------------------------*/
- static Boolean HandleMouseDown(EventRecord theEvent)
- {
- WindowPtr whichWindow;
- Point where;
- short whichPart;
- Rect boundsRect;
- GrafPtr oldPort;
- OSErr err = noErr;
- Boolean returnVal = false;
-
- whichPart = FindWindow (theEvent.where, &whichWindow);
- where = theEvent.where;
-
- GetPort(&oldPort);
- SetPort(whichWindow);
-
- GlobalToLocal(&where);
-
- switch (whichPart)
- {
- case inMenuBar:
- returnVal = DoMenuSelection(MenuSelect (theEvent.where));
- break;
-
- case inSysWindow:
- SystemClick (&theEvent, whichWindow);
- break;
-
- case inDrag:
- boundsRect = (*GetGrayRgn())->rgnBBox;
- DragWindow(whichWindow, theEvent.where, &boundsRect);
- break;
-
- case inGoAway:
- if (TrackGoAway(whichWindow, where))
- returnVal = DoClose(whichWindow);
- break;
-
- case inGrow:
- break;
-
- case inZoomIn:
- case inZoomOut:
- break;
-
- case inContent:
- SelectWindow(whichWindow);
- default:
- break;
- }
-
- SetPort(oldPort);
-
- return returnVal;
- }
-
- /*-------------------------HandleKeyDown-------------------------*/
- /* Handle key presses. Check to see if it's a command key, and if so,
- handle it and return 0.
- */
- static Boolean HandleKeyDown(EventRecord theEvent)
- {
- if (theEvent.modifiers & cmdKey)
- return DoMenuSelection (MenuKey ((char)theEvent.message & charCodeMask));
-
- return false;
- }
-
- /*-------------------------DoEventLoop-------------------------*/
- static void DoEventLoop(Boolean *outOptionKey)
- {
- EventRecord localEvent;
-
- while (!gQuitFlag)
- {
- if (WaitNextEvent(everyEvent, &localEvent, 20, nil))
- {
-
- if (IsDialogEvent(&localEvent))
- {
- DialogPtr dlg;
- short itemHit;
-
- if (DialogSelect(&localEvent, &dlg, &itemHit))
- {
- HandleDialogEvent(dlg, itemHit);
- continue;
- }
- }
-
- switch (localEvent.what)
- {
- case mouseDown:
- gQuitFlag = HandleMouseDown(localEvent);
- break;
-
- case keyDown:
- case autoKey:
- gQuitFlag = HandleKeyDown(localEvent);
- break;
-
- case diskEvt:
- /*Handle a disk insert. Checks for duff disks*/
- if (HiWord(localEvent.message)) {
- Point diskInitPt;
-
- diskInitPt.v = diskInitPt.h = 100;
- DILoad();
- DIBadMount(diskInitPt, localEvent.message);
- DIUnload();
- }
- break;
-
- case updateEvt:
- DoUpdateEvent(localEvent);
- break; /*Do nothing*/
-
- case activateEvt:
- DoActivateEvent(localEvent);
- break;
-
- case osEvt:
- DoOSEvent(localEvent);
- break;
-
- case nullEvent:
- DoIdleEvent();
- break; /*Do nothing*/
-
- case kHighLevelEvent:
- //DoHighLevelEvent(localEvent);
- break;
-
- default:
- break; /*Do nothing*/
-
- } /*Switch*/
- } else {
- DoIdleEvent();
- }
-
- } /*while*/
-
- *outOptionKey = (localEvent.modifiers & optionKey) != 0;
- }
-
-
- /*-------------------------SetupMenus-------------------------*/
- static void SetupMenus (void)
- {
-
- gAppleMenu = GetMenu (kAppleMenuID);
- AppendResMenu (gAppleMenu, 'DRVR' );
- InsertMenu (gAppleMenu, 0);
-
- gFileMenu = GetMenu ( kFileMenuID );
- InsertMenu (gFileMenu, 0);
-
- gEditMenu = GetMenu ( kEditMenuID );
- InsertMenu (gEditMenu, 0);
-
- DrawMenuBar ();
- }
-
- /*-------------------------InitToolbox-------------------------*/
- static void InitToolbox (void)
- {
- InitGraf ( &qd.thePort );
- InitFonts ();
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs (NULL);
- InitCursor ();
- FlushEvents ( everyEvent, 0 );
-
- MoreMasters ();
- MoreMasters ();
- }
-
-
- /*----------------------------------------------------------------------------
- main
-
- ----------------------------------------------------------------------------*/
-
- void main(void)
- {
- TPrefsData prefsData = {0};
- Boolean optionKeyOnQuit = false;
- OSErr err = noErr;
-
- InitToolbox();
- SetupMenus();
-
- err = GetMousePrefs(&prefsData);
- if (err != noErr) return;
-
- // this sets the data live, so no need to save it out.
- DisplayMouseDialog(&prefsData);
-
- DoEventLoop(&optionKeyOnQuit);
-
- if (optionKeyOnQuit) // let's be really evil,and choose a random angle
- {
- double randNum = (double)Random() / 32767.0;
- double randomAngle = kPi * randNum;
-
- SetMousePrefs(randomAngle);
- }
- }
-
-